package org.child_motivation.integration.dao;

import org.child_motivation.business.model.*;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JdbcTaskDao extends JdbcDaoSupport implements TaskDao {

    @Override
    public void saveOrUpdate(Task task) {
        Map<String, Object> params = new HashMap();
        params.put("name", task.getName());
        if(task.isNew()){
            SimpleJdbcInsert insert = new SimpleJdbcInsert(getJdbcTemplate());
            insert.setTableName("defaulttask");
            insert.setGeneratedKeyName("id");
            Number key = insert.executeAndReturnKey(params);
            task.setId(key.intValue());
        }else{
            params.put("id", task.getId());
            String sql = "UPDATE task SET name = name, WHERE id = id";
            getJdbcTemplate().update(sql,params);
        }


    }

    @Override
    public List<Task> getAllDefaultTasks() {
        String sql = "SELECT * FROM defaulttask";
        return getJdbcTemplate().query(sql, new TaskRowMapper());
    }

    @Override
    public void saveOrUpdate(PersonalTask task) {

        Map<String, Object> params = new HashMap();
        params.put("name", task.getName());
        params.put("description", task.getDescription());
        params.put("pocket_money_group_id", task.getGroup().getId());
        params.put("value", task.getValue());
        params.put("taskvalue", task.getTaskValue());

        if(task.isNew()){
            SimpleJdbcInsert insert = new SimpleJdbcInsert(getJdbcTemplate());
            insert.setTableName("task");
            insert.setGeneratedKeyName("id");
            Number key = insert.executeAndReturnKey(params);
            task.setId(key.intValue());
        }else{
            params.put("id", task.getId());
            String sql = "UPDATE task SET name = name, description = description, pocket_money_group_id = pocket_money_group_id, value = value, taskvalue = taskvalue WHERE id = id";
            getJdbcTemplate().update(sql,params);
        }


    }

    @Override
    public List<PersonalTask> getTaskForGroup(int groupid) {
        String sql = "SELECT * FROM task WHERE id = ?";
        return getJdbcTemplate().query(sql, new PersonalTaskRowMapper(), groupid);
    }

    @Override
    public void saveOrUpdate(TaskForApproval task) {

    }

    @Override
    public List<TaskForApproval> getApprovalTasksForGroup(int groupid) {
        return null;
    }

    private class TaskRowMapper implements ParameterizedRowMapper<Task> {
        public Task mapRow(ResultSet rs, int i) throws SQLException {
            int id = rs.getInt("id");
            String name = rs.getString("name");

            return new Task(id, name);
        }

    }
    private class PersonalTaskRowMapper implements ParameterizedRowMapper<PersonalTask> {
        public PersonalTask mapRow(ResultSet rs, int i) throws SQLException {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String description = rs.getString("description");
            int groupid = rs.getInt("pocket_money_group_id");
            Group group = new Group();
            group.setId(groupid);
            double value = rs.getDouble("value");
            TaskValue taskvalue = TaskValue.valueOf(rs.getString("taskvalue"));
            return new PersonalTask(id, name, description, group,value, taskvalue);

        }

    }
}
